package com.zft.userwalletmanager.web.controller;


import com.zft.userwalletmanager.infrastructure.common.enums.ResultCode;
import com.zft.userwalletmanager.infrastructure.common.exception.BizException;
import com.zft.userwalletmanager.web.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 统一异常处理
 */
@RestControllerAdvice
@Slf4j
public class ExceptionController {
    @ExceptionHandler
    public Result handler(Exception e) {
        if (e instanceof IllegalArgumentException) {
            IllegalArgumentException illegalArgumentException = (IllegalArgumentException) e;
            log.error("IllegalArgumentException:", e);
            return Result.fail(ResultCode.PARAM_ERROR,illegalArgumentException.getMessage());
        }
        if (e instanceof BizException) {
            BizException bizException = (BizException) e;
            log.error("BizException:", e);
            return Result.fail(bizException.getResultCode());
        }
        log.error("Exception:", e);
        return Result.fail();
    }
}
